package com.serviceprovider.business.module.admin.user.dao.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.address.model.Address;
import com.serviceprovider.business.module.admin.role.model.Role;
import com.serviceprovider.business.module.user.model.impl.User;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

//~--- JDK imports ------------------------------------------------------------

import java.sql.ResultSet;
import java.sql.SQLException;

public class UserMapper implements ParameterizedRowMapper<User> {
    @Override
    public User mapRow(ResultSet rs, int arg1) throws SQLException {
        User user = new User();

        user.setId(rs.getInt("id"));
        user.setEmail(rs.getString("email"));
        user.setAlternateEmail(rs.getString("alternateEmail"));
        user.setPassword(rs.getString("password"));
        user.setEnabled(rs.getBoolean("enabled"));
        user.setDeleted(rs.getBoolean("deleted"));
        user.setRefererId(rs.getInt("referer_id"));
        user.setFirstName(rs.getString("firstName"));
        user.setMilddleName(rs.getString("milddleName"));
        user.setLastName(rs.getString("lastName"));
        user.setEfin(rs.getInt("efin"));
        user.setPtin(rs.getInt("ptin"));
        user.setSsn(rs.getInt("ssn"));
        user.setOrderApproval(rs.getInt("orderApproval"));
        user.setCheckPrintingBy(rs.getInt("checkPrintingBy"));
        user.setBusinessName(rs.getString("businessName"));
        user.setBusinessFein(rs.getString("businessFein"));
        user.setOwnerSsn(rs.getInt("ownerssn"));
        user.setPrefMessageNotifyType(rs.getInt("pref_message_notify_type"));
        user.setPrefMessageNotifyAlert(rs.getInt("pref_message_notify_alert"));
        user.setBonusTestAmount(rs.getInt("bonusTestAmount"));
        user.setCreatedOn(rs.getDate("createdOn"));
        user.setInGui(rs.getInt("inGui") != 0);

        int level1UserId      = rs.getInt("Level_1_UserID");
        int roleId            = rs.getInt("role");
        int contactAddressId  = rs.getInt("contactAddressId");
        int businessAddressId = rs.getInt("businessAddressId");
        int shippingAddressId = rs.getInt("shippingAddressId");

        if (level1UserId != 0) {
            user.setLevel1User(new User());
            user.getLevel1User().setId(level1UserId);
        }

        if (roleId != 0) {
            user.setRole(new Role());
            user.getRole().setId(roleId);
        }

        if (contactAddressId != 0) {
            user.setContactAddress(new Address());
            user.getContactAddress().setId(contactAddressId);
        }

        if (businessAddressId != 0) {
            user.setBusinessAddress(new Address());
            user.getBusinessAddress().setId(businessAddressId);
        }

        if (shippingAddressId != 0) {
            user.setShippingAddress(new Address());
            user.getShippingAddress().setId(shippingAddressId);
        }

        return user;
    }
}



